package action.creditapproval;

import java.sql.Timestamp;
import java.util.List;

import model.app.AppRate;
import model.credit.Creditapplyenterprise;
import model.credit.Creditapplypersonal;
import model.creditcheck.Applyopinion;
import model.creditcheck.Applystate;
import model.security.UsrUserRole;

import service.applycheck.AppRateService;
import service.applycheck.ApplyOpinionService;
import service.applycheck.ApplyStateService;
import service.applycheck.ApplyUserService;
import service.credit.CreditApplyEnterpriseService;
import service.credit.CreditApplyPersonalService;
import util.Log;
import util.Time;
import vo.CreditCheckBean;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

public class CreditApprovalAction extends BaseAction {
	private String state;
	private int type;
	private List<?> creditApprovalBeanList;
	private CreditCheckBean creditCheckBean;
	private List<?> recordList;

	private AppRate appRate;

	private Integer applyid;
	private Integer appid;
	private int isCanChange;

	private Integer setcheckstate;
	private String setopinion;

	private ApplyStateService applyStateService;
	private CreditApplyPersonalService creditApplyPersonalService;
	private CreditApplyEnterpriseService creditApplyEnterpriseService;
	private ApplyOpinionService applyOpinionService;
	private ApplyUserService applyUserService;

	private AppRateService appRateService;
	
	private int searchwhat;
	
	private String searchApplyid;

	public String execute() throws Exceptions {
		if (this.type == 1) {
			this.creditApprovalBeanList = this.creditApplyPersonalService
					.getAllApprovalList("wait");
		} else if (this.type == 2) {
			this.creditApprovalBeanList = this.creditApplyEnterpriseService
					.getAllApprovalList("wait");
		}
		this.searchwhat = 1;
		return "success";
	}
	
	public String searchList() throws Exceptions{
		
		try{
			this.applyid = Integer.parseInt(this.searchApplyid);
		}catch(Exception e){
			this.applyid = Integer.parseInt(this.searchApplyid.substring(3, this.searchApplyid.length()));
		}
		
		if(this.type == 1){
			this.creditApprovalBeanList = this.creditApplyPersonalService.getAllAcceptionListByApplyIdAndState(this.applyid, "approval");
		}else if(this.type == 2){
			this.creditApprovalBeanList = this.creditApplyEnterpriseService.getAllAcceptionListByApplyIdAndState(this.applyid, "approval");
		}
		return "success";
	}

	public String applyApproval() throws Exceptions {
		if (this.type == 1) {
			creditCheckBean = this.creditApplyPersonalService
					.getOneBean(applyid);
			recordList = this.applyOpinionService.getListAllByApplyId(applyid);
			if (this.isCanChange == 1 || this.isCanChange == 2) {
				this.appRate = this.appRateService.getAppRate();
			}
		} else if (this.type == 2) {
			creditCheckBean = this.creditApplyEnterpriseService
					.getOneBean(applyid);
			recordList = this.applyOpinionService.getListAllByApplyId(applyid);
			if (this.isCanChange == 1 || this.isCanChange == 2) {
				this.appRate = this.appRateService.getAppRate();
			}
		}
		return "detail";
	}
	
	public void doApprovalPersonal() throws Exceptions{
		String result = "";
		Timestamp nowTime = Time.CurrentTimestamp();
		UsrUserRole user = applyUserService.getOneByUserRoleId(5);
		Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService
				.getOneById(appid);
		Applystate _applystate = applyStateService
				.getOneApplyStateByIdAndType(appid, "C");
		try {
			if (this.setcheckstate == 1) {
				result = "同意";
				/*
				 * if(this.applyStateService.checkApplyState(appid) == 1){
				 * applyStateService
				 * .saveAllByManagerId(_creditapplypersonal.getApplyid(),
				 * user.getUsrUser().getUserid().intValue(), "D", this.type,
				 * _creditapplypersonal.getCount()); }
				 */
				/*
				 * if(applyStateService.isHaveFP(appid) == 0){
				 * applyStateService
				 * .saveAllByManagerId(_creditapplypersonal.getApplyid(),
				 * user.getUsrUser().getUserid().intValue(), "D",
				 * this.type); }
				 */
			} else if (this.setcheckstate == 2) {
				result = "不同意";
			} else if (this.setcheckstate == 12) {
				result = "复议";
			} else if (this.setcheckstate == 3) {
				result = "回退";
			}
			_applystate.setCheckstate(setcheckstate);
			_applystate.setCheckopinion(setopinion);
			_applystate.setChecktime(nowTime);
			applyStateService.save(_applystate);
			if (this.setcheckstate == 3) {
				if (this.applyStateService.getBackStateCheck(appid) == 1) {
					if (_creditapplypersonal.getCreditstate() == 32) {
						_creditapplypersonal.setCreditstate(22);
					} else {
						throw new Exception("该操作已经被其他审查人员操作，请刷新页面!");
					}
				}
			} else {
				int num = this.applyStateService.checkApplyState(appid);
				if (num == 1) {
					_creditapplypersonal.setCreditstate(41);
					_creditapplypersonal.setIsdone(3);
					applyStateService.saveAllByManagerId(appid, user
							.getUsrUser().getUserid().intValue(), "D",
							this.type, _creditapplypersonal.getCount());
				} else if (num == 2) {          /*判断两次不同意之后直接拉黑申请*/
					if(_creditapplypersonal.getCount() >= 2){
						_creditapplypersonal.setCreditstate(40);
					}else{
						_creditapplypersonal.setCreditstate(22);
						_creditapplypersonal.setIsdone(3);
					}
				} else if (num == 10) {
					_creditapplypersonal.setCreditstate(40);
					_creditapplypersonal.setIsdone(3);
				}
			}
			creditApplyPersonalService.save(_creditapplypersonal);
			this.applyOpinionService.save(new Applyopinion(appid, Global
					.userid().intValue(), null, "审批", setopinion, result,
					nowTime, this.type, _creditapplypersonal.getCount()));
			Log.save("插入", "个人审批申请单编号为：" + _creditapplypersonal.getApplyid(), "操作人员为：" + Global.userid());
		} catch (Exception e) {
			throw new Exceptions("在执行过程中出现了错误了!");
		}
	}

	public void doApprovalEnterprise() throws Exceptions{
		String result = "";
		Timestamp nowTime = Time.CurrentTimestamp();
		UsrUserRole user = applyUserService.getOneByUserRoleId(5);
		Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService
				.getOneById(appid);
		Applystate _applystate = applyStateService
				.getOneApplyStateByIdAndType(appid, "C");
		try {
			if (this.setcheckstate == 1) {
				result = "同意";
				/*
				 * if(applyStateService.isHaveFP(appid) == 0){
				 * applyStateService
				 * .saveAllByManagerId(_creditapplyenterprise.getApplyid(),
				 * user.getUsrUser().getUserid().intValue(), "D",
				 * this.type); }
				 */
			} else if (this.setcheckstate == 2) {
				result = "不同意";
			} else if (this.setcheckstate == 12) {
				result = "复议";
			} else if (this.setcheckstate == 3) {
				result = "回退";
			}
			_applystate.setCheckstate(setcheckstate);
			_applystate.setCheckopinion(setopinion);
			_applystate.setChecktime(nowTime);
			applyStateService.save(_applystate);
			if (this.setcheckstate == 3) {
				if (this.applyStateService.getBackStateCheck(appid) == 1) {
					if (_creditapplyenterprise.getCreditstate() == 32) {
						_creditapplyenterprise.setCreditstate(22);
					} else {
						throw new Exception("该操作已经被其他审查人员操作，请刷新页面!");
					}
				}
			} else {
				int num = this.applyStateService.checkApplyState(appid);
				if (num == 1) {
					_creditapplyenterprise.setCreditstate(41);
					_creditapplyenterprise.setIsdone(3);
					applyStateService.saveAllByManagerId(appid, user
							.getUsrUser().getUserid().intValue(), "D",
							this.type, _creditapplyenterprise.getCount());
				} else if (num == 2) {
					if(_creditapplyenterprise.getCount() >= 2){           /*判断两次不同意之后直接拉黑申请*/
						_creditapplyenterprise.setCreditstate(40);
					}else{
						_creditapplyenterprise.setCreditstate(22);
						_creditapplyenterprise.setIsdone(3);
					}
				} else if (num == 10) {
					_creditapplyenterprise.setCreditstate(40);
					_creditapplyenterprise.setIsdone(3);
				}
			}
			creditApplyEnterpriseService.save(_creditapplyenterprise);
			this.applyOpinionService.save(new Applyopinion(appid, Global
					.userid().intValue(), null, "审批", setopinion, result,
					nowTime, this.type, _creditapplyenterprise.getCount()));
			Log.save("插入", "企业审批申请单编号为：" + _creditapplyenterprise.getApplyid(), "操作人员为：" + Global.userid());
		} catch (Exception e) {
			throw new Exceptions("在执行过程中出现了错误了!");
		}
	}
	
	public String doApproval() throws Exception {
		if(this.type == 1){
			this.doApprovalPersonal();
		}else if(this.type == 2){
			this.doApprovalEnterprise();
		}else{
			throw new Exception("你的操作存在失误。请按照要求来!");
		}
		MessageJump("信息审批完成", "creditApprovalAction?type=" + this.type);
		return "message";
	}
	
	/*public String doApproval() throws Exception {
		String result = "";
		Timestamp nowTime = Time.CurrentTimestamp();
		UsrUserRole user = applyUserService.getOneByUserRoleId(5);
		if (this.type == 1) {
			Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService
					.getOneById(appid);
			Applystate _applystate = applyStateService
					.getOneApplyStateByIdAndType(appid, "C");
			try {
				if (this.setcheckstate == 1) {
					result = "同意";
				} else if (this.setcheckstate == 2) {
					result = "不同意";
				} else if (this.setcheckstate == 12) {
					result = "复议";
				} else if (this.setcheckstate == 3) {
					result = "回退";
				}
				_applystate.setCheckstate(setcheckstate);
				_applystate.setCheckopinion(setopinion);
				_applystate.setChecktime(nowTime);
				applyStateService.save(_applystate);
				if (this.setcheckstate == 3) {
					if (this.applyStateService.getBackStateCheck(appid) == 1) {
						if (_creditapplypersonal.getCreditstate() == 32) {
							_creditapplypersonal.setCreditstate(22);
						} else {
							throw new Exception("该操作已经被其他审查人员操作，请刷新页面!");
						}
					}
				} else {
					int num = this.applyStateService.checkApplyState(appid);
					if (num == 1) {
						_creditapplypersonal.setCreditstate(41);
						_creditapplypersonal.setIsdone(3);
						applyStateService.saveAllByManagerId(appid, user
								.getUsrUser().getUserid().intValue(), "D",
								this.type, _creditapplypersonal.getCount());
					} else if (num == 2) {          判断两次不同意之后直接拉黑申请
						if(_creditapplypersonal.getCount() >= 2){
							_creditapplypersonal.setCreditstate(40);
						}else{
							_creditapplypersonal.setCreditstate(22);
							_creditapplypersonal.setIsdone(3);
						}
					} else if (num == 10) {
						_creditapplypersonal.setCreditstate(40);
						_creditapplypersonal.setIsdone(3);
					}
				}
				creditApplyPersonalService.save(_creditapplypersonal);
				this.applyOpinionService.save(new Applyopinion(appid, Global
						.userid().intValue(), null, "审批", setopinion, result,
						nowTime, this.type, _creditapplypersonal.getCount()));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		} else if (this.type == 2) {
			Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService
					.getOneById(appid);
			Applystate _applystate = applyStateService
					.getOneApplyStateByIdAndType(appid, "C");
			try {
				if (this.setcheckstate == 1) {
					result = "同意";
				} else if (this.setcheckstate == 2) {
					result = "不同意";
				} else if (this.setcheckstate == 12) {
					result = "复议";
				} else if (this.setcheckstate == 3) {
					result = "回退";
				}
				_applystate.setCheckstate(setcheckstate);
				_applystate.setCheckopinion(setopinion);
				_applystate.setChecktime(nowTime);
				applyStateService.save(_applystate);
				if (this.setcheckstate == 3) {
					if (this.applyStateService.getBackStateCheck(appid) == 1) {
						if (_creditapplyenterprise.getCreditstate() == 32) {
							_creditapplyenterprise.setCreditstate(22);
						} else {
							throw new Exception("该操作已经被其他审查人员操作，请刷新页面!");
						}
					}
				} else {
					int num = this.applyStateService.checkApplyState(appid);
					if (num == 1) {
						_creditapplyenterprise.setCreditstate(41);
						_creditapplyenterprise.setIsdone(3);
						applyStateService.saveAllByManagerId(appid, user
								.getUsrUser().getUserid().intValue(), "D",
								this.type, _creditapplyenterprise.getCount());
					} else if (num == 2) {
						if(_creditapplyenterprise.getCount() >= 2){           判断两次不同意之后直接拉黑申请
							_creditapplyenterprise.setCreditstate(40);
						}else{
							_creditapplyenterprise.setCreditstate(22);
							_creditapplyenterprise.setIsdone(3);
						}
					} else if (num == 10) {
						_creditapplyenterprise.setCreditstate(40);
						_creditapplyenterprise.setIsdone(3);
					}
				}
				creditApplyEnterpriseService.save(_creditapplyenterprise);
				this.applyOpinionService.save(new Applyopinion(appid, Global
						.userid().intValue(), null, "审批", setopinion, result,
						nowTime, this.type, _creditapplyenterprise.getCount()));
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}
		MessageJump("信息审查完成", "creditApprovalAction?type=" + this.type);
		return "message";
	}*/
	
	public String cdw() throws Exceptions {
		if (this.type == 1) {
			this.creditApprovalBeanList = this.creditApplyPersonalService
					.getAllApprovalList("wait");
		} else if (this.type == 2) {
			this.creditApprovalBeanList = this.creditApplyEnterpriseService
					.getAllApprovalList("wait");
		}
		this.searchwhat = 1;
		return "success";
	}

	public String cdd() throws Exceptions {
		if (this.type == 1) {
			this.creditApprovalBeanList = this.creditApplyPersonalService
					.getAllApprovalList("doing");
		} else if (this.type == 2) {
			this.creditApprovalBeanList = this.creditApplyEnterpriseService
					.getAllApprovalList("doing");
		}
		this.searchwhat = 2;
		return "success";
	}

	public String cdb() throws Exceptions {
		if (this.type == 1) {
			this.creditApprovalBeanList = this.creditApplyPersonalService
					.getAllApprovalList("back");
		} else if (this.type == 2) {
			this.creditApprovalBeanList = this.creditApplyEnterpriseService
					.getAllApprovalList("back");
		}
		this.searchwhat = 3;
		return "success";
	}

	public String cdr() throws Exceptions {
		if (this.type == 1) {
			this.creditApprovalBeanList = this.creditApplyPersonalService
					.getAllApprovalList("returnBack");
		} else if (this.type == 2) {
			this.creditApprovalBeanList = this.creditApplyEnterpriseService
					.getAllApprovalList("returnBack");
		}
		this.searchwhat = 4;
		return "success";
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public List<?> getCreditApprovalBeanList() {
		return creditApprovalBeanList;
	}

	public void setCreditApprovalBeanList(List<?> creditApprovalBeanList) {
		this.creditApprovalBeanList = creditApprovalBeanList;
	}

	public CreditCheckBean getCreditCheckBean() {
		return creditCheckBean;
	}

	public void setCreditCheckBean(CreditCheckBean creditCheckBean) {
		this.creditCheckBean = creditCheckBean;
	}

	public ApplyStateService getApplyStateService() {
		return applyStateService;
	}

	public void setApplyStateService(ApplyStateService applyStateService) {
		this.applyStateService = applyStateService;
	}

	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}

	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}

	public CreditApplyEnterpriseService getCreditApplyEnterpriseService() {
		return creditApplyEnterpriseService;
	}

	public void setCreditApplyEnterpriseService(
			CreditApplyEnterpriseService creditApplyEnterpriseService) {
		this.creditApplyEnterpriseService = creditApplyEnterpriseService;
	}

	public List<?> getRecordList() {
		return recordList;
	}

	public void setRecordList(List<?> recordList) {
		this.recordList = recordList;
	}

	public ApplyOpinionService getApplyOpinionService() {
		return applyOpinionService;
	}

	public void setApplyOpinionService(ApplyOpinionService applyOpinionService) {
		this.applyOpinionService = applyOpinionService;
	}

	public int getIsCanChange() {
		return isCanChange;
	}

	public void setIsCanChange(int isCanChange) {
		this.isCanChange = isCanChange;
	}

	public AppRate getAppRate() {
		return appRate;
	}

	public void setAppRate(AppRate appRate) {
		this.appRate = appRate;
	}

	public AppRateService getAppRateService() {
		return appRateService;
	}

	public void setAppRateService(AppRateService appRateService) {
		this.appRateService = appRateService;
	}

	public Integer getApplyid() {
		return applyid;
	}

	public void setApplyid(Integer applyid) {
		this.applyid = applyid;
	}

	public Integer getAppid() {
		return appid;
	}

	public void setAppid(Integer appid) {
		this.appid = appid;
	}

	public Integer getSetcheckstate() {
		return setcheckstate;
	}

	public void setSetcheckstate(Integer setcheckstate) {
		this.setcheckstate = setcheckstate;
	}

	public String getSetopinion() {
		return setopinion;
	}

	public void setSetopinion(String setopinion) {
		this.setopinion = setopinion;
	}

	public ApplyUserService getApplyUserService() {
		return applyUserService;
	}

	public void setApplyUserService(ApplyUserService applyUserService) {
		this.applyUserService = applyUserService;
	}

	public int getSearchwhat() {
		return searchwhat;
	}

	public void setSearchwhat(int searchwhat) {
		this.searchwhat = searchwhat;
	}

	public String getSearchApplyid() {
		return searchApplyid;
	}

	public void setSearchApplyid(String searchApplyid) {
		this.searchApplyid = searchApplyid;
	}

}
